/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.TrainKhdz;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.service.TrainKhdzService;
import me.zhengjie.domain.vo.TrainKhdzQueryCriteria;
import me.zhengjie.mapper.TrainKhdzMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author huangwencong
* @date 2024-10-05
**/
@Service
@RequiredArgsConstructor
public class TrainKhdzServiceImpl extends ServiceImpl<TrainKhdzMapper, TrainKhdz> implements TrainKhdzService {

    private final TrainKhdzMapper trainKhdzMapper;

    @Override
    public TrainKhdz findOne(Long trainId, Long trainItemId) {
        return trainKhdzMapper.findOne(trainId, trainItemId);
    }

    @Override
    public PageResult<TrainKhdz> queryAll(TrainKhdzQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(trainKhdzMapper.findAll(criteria, page));
    }

    @Override
    public List<TrainKhdz> queryAll(TrainKhdzQueryCriteria criteria){
        return trainKhdzMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(TrainKhdz resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TrainKhdz resources) {
        TrainKhdz trainKhdz = getById(resources.getId());
        trainKhdz.copy(resources);
        saveOrUpdate(trainKhdz);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<TrainKhdz> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TrainKhdz trainKhdz : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("训练 ID", trainKhdz.getTrainId());
            map.put("训练项目", trainKhdz.getTrainItemId());
            map.put("心眼垂直距离(cm)", trainKhdz.getCycz());
            map.put("安静血压(mmhg)", trainKhdz.getAjxy());
            map.put("HP抗荷动作血压(mmHg)-无抗荷服", trainKhdz.getHpxyWf());
            map.put("HP抗荷动作血压(mmHg)-有抗荷服", trainKhdz.getHpxy());
            map.put("下肢蹬力检测(合格/不合格)", trainKhdz.getXzdl());
            map.put("左下肢(kg)", trainKhdz.getZxz());
            map.put("右下肢(kg)", trainKhdz.getYxz());
            map.put("持续时间(s)", trainKhdz.getCxsj());
            map.put("HP 动作掌握程度(0:熟练掌握，1:掌握，2:基本掌握，3:不掌握)", trainKhdz.getHpzw());
            map.put("呼吸曲线与呼吸引导曲线同步程度(0:完全同步，1:同步，2:基本同步，3:不同步)", trainKhdz.getQxtb());
            map.put("提高 G值((训练收缩压-安静收缩呀)/心眼距离/1.28)", trainKhdz.getG());
            map.put("提高 G值无抗荷服((训练收缩压-安静收缩呀)/心眼距离/1.28)", trainKhdz.getGWf());
            map.put("评定结果(0:优秀，1:良好，2:合格，3:不合格)", trainKhdz.getResult());
            map.put("建议", trainKhdz.getJy());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}